var express = require('express')
var router = express.Router()

const admin = require('../models/admin')
const menu = require('../models/menu')
const role = require('../models/role')
const { tokenVerification } = require('../middleware/index.js')

// 查询
router.get('/info', async (req, res, next) => {
	let user = await tokenVerification(req.headers.token)
	let adminParams = {
		username: user.username
	}
	const adminData = await admin.findOne({ where: adminParams })
	let roleParams = {
		id: adminData.role
	}
	const roleData = await role.findOne({ where: roleParams })
	let menuParams = {
		id: roleData.menu.split(','),
		type: 3
	}
	const menuData = await menu.findAll({ where: menuParams })
	let permissionsArr = menuData.map(item => {
		return item.permission
	})
	res.json({
		code: 0,
		data: { permissions: permissionsArr, sysUser: adminData },
		msg: 'success'

	})
})

module.exports = router
